package com.zd.wechatPlatform.common.utils;

import org.springframework.util.StringUtils;

public class WeixinInterfaceUtil {
    public static final String GEN_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    public static final String SEND_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
    public static final String GET_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info";
    public static final String GET_OAUTH_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
    public static final String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
    public static final String GET_MEDIA_URL = "http://file.api.weixin.qq.com/cgi-bin/media/get";
    public static final String SEND_TEMP_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send";
    public static final String GEN_SHORT_URL_URL = "https://api.weixin.qq.com/cgi-bin/shorturl";
    public static final String GET_TAGS_URL = "https://api.weixin.qq.com/cgi-bin/tags/get";
    public static final String DELETE_TAGS_URL = "https://api.weixin.qq.com/cgi-bin/tags/delete";
    public static final String CREATE_TAG_URL = "https://api.weixin.qq.com/cgi-bin/tags/create";

    public static final String BATCHTAGGING_URL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging";
    public static final String BATCHUNTAGGING_URL = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging";

    public static String genAccessToken(String appid, String appsecret) {
        StringBuffer genAccessTokenUrl = new StringBuffer(GEN_ACCESS_TOKEN_URL);
        genAccessTokenUrl.append("&appid=" + appid);
        genAccessTokenUrl.append("&secret=" + appsecret);
        return OkHttpUtil.getSync(genAccessTokenUrl.toString());
    }
    
    public static String genJsapiTikcet(String accessToken) {
        StringBuffer genJsapiTikcetUrl = new StringBuffer(GET_JSAPI_TICKET_URL);
        genJsapiTikcetUrl.append("&access_token=" + accessToken);
        return OkHttpUtil.getSync(genJsapiTikcetUrl.toString());
    }

    public static String getUserInfo(String accessToken, String openid, String lang) {
        StringBuffer getUserInfoUrl = new StringBuffer(GET_USER_INFO_URL);
        getUserInfoUrl.append("?access_token=" + accessToken);
        getUserInfoUrl.append("&openid=" + openid);
        if (!StringUtils.isEmpty(lang)) {
            getUserInfoUrl.append("&lang=" + lang);
        }
        return OkHttpUtil.getSync(getUserInfoUrl.toString());
    }

    public static String getOauthAccessToken(String appid, String appsecret, String code) {
        StringBuffer getOauthAccessTokenUrl = new StringBuffer(GET_OAUTH_ACCESS_TOKEN_URL);
        getOauthAccessTokenUrl.append("?appid=" + appid);
        getOauthAccessTokenUrl.append("&secret=" + appsecret);
        getOauthAccessTokenUrl.append("&code=" + code);
        getOauthAccessTokenUrl.append("&grant_type=authorization_code");
        return OkHttpUtil.getSync(getOauthAccessTokenUrl.toString());
    }

    public static String sendTempMsg(String accessToken, String parameterJson) {
        LogUtils.debug("accessToken:" + accessToken);
        LogUtils.debug("parameterJson:" + parameterJson);
        StringBuffer sendTempMsgUrl = new StringBuffer(SEND_TEMP_MSG_URL);
        sendTempMsgUrl.append("?access_token=" + accessToken);
        return OkHttpUtil.postSync(sendTempMsgUrl.toString(), parameterJson).toString();
    }

    public static String getTags(String accessToken) {
        StringBuffer url = new StringBuffer(GET_TAGS_URL);
        url.append("?access_token=" + accessToken);
        return OkHttpUtil.getSync(url.toString());
    }

    public static String deleteTags(String accessToken, Integer tagId) {
        StringBuffer url = new StringBuffer(DELETE_TAGS_URL);
        url.append("?access_token=" + accessToken);
        String parameterJson = "{   \"tag\":{        \"id\" : " + tagId + "   } }";
        LogUtils.debug(parameterJson);
        return OkHttpUtil.postSync(url.toString(), parameterJson).toString();
    }

    public static String createTag(String accessToken, String tagName) {
        StringBuffer url = new StringBuffer(CREATE_TAG_URL);
        url.append("?access_token=" + accessToken);
        String parameterJson = "{   \"tag\" : {     \"name\" : \"" + tagName + "\"   } }";
        LogUtils.debug(parameterJson);
        return OkHttpUtil.postSync(url.toString(), parameterJson).toString();
    }

    public static String sendMsg(String accessToken, String parameterJson) {
        LogUtils.debug("accessToken:" + accessToken + ",parameterJson:" + parameterJson);
        StringBuffer sendTempMsgUrl = new StringBuffer(SEND_MSG_URL);
        sendTempMsgUrl.append("?access_token=" + accessToken);
        return OkHttpUtil.postSync(sendTempMsgUrl.toString(), parameterJson).toString();
    }

    /**
     * 标签功能目前支持公众号为用户打上最多20个标签。
     *
     * @param accessToken
     * @param parameterJson {   "openid_list" : [//粉丝列表
     *                      "ocYxcuAEy30bX0NXmGn4ypqx3tI0",
     *                      "ocYxcuBt0mRugKZ7tGAHPnUaOW7Y"   ],
     *                      "tagid" : 134 }
     * @return 返回结果格式如下
     * {
     * "errcode":0,
     * "errmsg":"ok"
     * }
     */
    public static String batchtagging(String accessToken, String parameterJson) {
        LogUtils.debug("accessToken:" + accessToken + ",parameterJson:" + parameterJson);
        StringBuffer url = new StringBuffer(BATCHTAGGING_URL);
        url.append("?access_token=" + accessToken);
        return OkHttpUtil.postSync(url.toString(), parameterJson).toString();
    }


    /**
     * 标签功能目前支持公众号为用户打上最多20个标签。
     *
     * @param accessToken
     * @param parameterJson {   "openid_list" : [//粉丝列表
     *                      "ocYxcuAEy30bX0NXmGn4ypqx3tI0",
     *                      "ocYxcuBt0mRugKZ7tGAHPnUaOW7Y"   ],
     *                      "tagid" : 134 }
     * @return 返回结果格式如下
     * {
     * "errcode":0,
     * "errmsg":"ok"
     * }
     */
    public static String batchuntagging(String accessToken, String parameterJson) {
        LogUtils.debug("accessToken:" + accessToken + ",parameterJson:" + parameterJson);
        StringBuffer url = new StringBuffer(BATCHUNTAGGING_URL);
        url.append("?access_token=" + accessToken);
        return OkHttpUtil.postSync(url.toString(), parameterJson).toString();
    }
}
